package com.gewu.pm.ui.fragment.report;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.bean.CommentBean;
import com.gewu.pm.bean.DailyReportBean;
import com.gewu.pm.ui.adapter.DailyReportAdapter;
import com.gewu.pm.ui.popup.SelectTimePopup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 日报 Fragment
 */
public final class DailyReportFragment extends AppFragment<AppActivity>
        implements DailyReportAdapter.OnCommentClickListener {

    private TextView mTypeView;
    private RecyclerView mRecyclerView;
    private DailyReportAdapter mAdapter;
    private SelectTimePopup.Builder mTypePopup;

    private int mCurrentType = 0; // 当前类型：0=全部，1=日报，2=拜访
    private List<String> mTypeList = Arrays.asList("全部", "日报", "拜访");

    public static DailyReportFragment newInstance() {
        return new DailyReportFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_daily_report;
    }

    @Override
    protected void initView() {
        mTypeView = findViewById(R.id.tv_type);
        mRecyclerView = findViewById(R.id.rv_list);

        // 设置类型选择器点击
        setOnClickListener(mTypeView);

        // 设置列表
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new DailyReportAdapter(getContext());
        mAdapter.setOnCommentClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        List<DailyReportBean> dataList = new ArrayList<>();
        
        // 创建1个日报数据
        DailyReportBean report1 = new DailyReportBean("张三", "日报", "每日工作汇报");
        report1.getImageList().add("https://example.com/image1.jpg");
        report1.getImageList().add("https://example.com/image2.jpg");
        report1.getImageList().add("https://example.com/image3.jpg");
        
        // 添加评论
        report1.getCommentList().add(new CommentBean("陈云伟", "评论的相关内容在这里", null));
        report1.getCommentList().add(new CommentBean("江达传", "评论的相关内容在这里", "陈云伟"));
        
        dataList.add(report1);
        
        // 创建2个拜访数据
        DailyReportBean visit1 = new DailyReportBean("李四", "拜访", "下去楼盘店（家装置店）");
        visit1.getImageList().add("https://example.com/visit1.jpg");
        visit1.getCommentList().add(new CommentBean("王五", "拜访工作做得不错", null));
        dataList.add(visit1);
        
        DailyReportBean visit2 = new DailyReportBean("赵六", "拜访", "下去楼盘店（家装置店）");
        visit2.getImageList().add("https://example.com/visit2.jpg");
        dataList.add(visit2);
        
        mAdapter.setData(dataList);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_type) {
            // 显示类型选择弹窗
            showTypePopup();
        }
    }

    /**
     * 显示类型选择弹窗
     */
    private void showTypePopup() {
        if (mTypePopup == null) {
            mTypePopup = new SelectTimePopup.Builder(getContext())
                    .setList(mTypeList)
                    .setSelectedPosition(mCurrentType)
                    .setListener((popup, data) -> {
                        int position = mTypeList.indexOf(data);
                        if (position != -1) {
                            mCurrentType = position;
                            mTypeView.setText(data);
                            // TODO: 根据类型筛选数据
                        }
                    });
        } else {
            mTypePopup.setSelectedPosition(mCurrentType);
        }
        mTypePopup.showAsDropDown(mTypeView);
    }

    @Override
    public void onCommentClick(int position, DailyReportBean item) {
        // 点击评论按钮，弹出软键盘
        toast("点击评论，弹出键盘");
        // 弹出软键盘
        showKeyboard();
    }

    /**
     * 弹出软键盘
     */
    private void showKeyboard() {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
        }
    }
}

